<template>
  <div class="courselist-container">
    <header>课程列表</header>
    <!-- 循环生成列表 -->
    <div class="courselist">
      <template v-for="(item, index) in courseList">
        <div class="course-item" :key="index + '-' + item.id">
          <div class="course-item-id">
            <span>ID:{{ item.id }}</span>
            <input
              type="checkbox"
              @change="checkChange"
              v-model="checkList"
              :value="item.id"
            />
          </div>
          <div class="img-box">
            <!-- 角标 -->
            <i
              class="subscript"
              :style="{ backgroundColor: item.type ? '#31a032' : '#fb3233' }"
              >{{ item.type ? "上架" : "下架" }}</i
            >
            <img :src="getImgUrl(item.src)" />
          </div>
          <div class="course-info">
            <h2 class="title">{{ item.title }}</h2>
            <p>报名费：{{ item.fee }}¥</p>
            <p>销量：{{ item.num }}</p>
            <p>上传人：{{ item.uploader }}</p>
            <p>上传时间：{{ item.uploadDate }}</p>
          </div>
          <div class="btnBox">
            <el-button
              class="editBtn"
              icon="el-icon-edit"
              @click="openDialog(item)"
              >编辑</el-button
            >
            <el-button
              class="deleteBtn"
              icon="el-icon-delete"
              @click="deleteByID([item.id])"
              >删除</el-button
            >
          </div>
        </div>
      </template>
    </div>

    <!-- 底部全选 和 批量删除 -->
    <div class="bottom-box">
      <label class="checkAll">
        <input type="checkbox" v-model="checkAll" @click="clickFun" />
        全选
      </label>
      <el-button type="text" @click="deleteByID(checkList)">批量删除</el-button>
    </div>
  </div>
</template>

<script>
import Bus from "../../../utils/bus";
export default {
  props: ["courseList"],
  data() {
    return {
      checkAll: false,
      checkList: [],
    };
  },
  computed: {
    idList() {
      return this.courseList.map((elm) => {
        return elm.id;
      });
    },
  },
  methods: {
    clickFun() {
      if (event.target.checked) {
        this.checkList = [...this.idList];
      } else {
        this.checkList = [];
      }
    },
    checkChange() {
      if (this.checkList.length === this.idList.length) {
        this.checkAll = true;
      } else {
        this.checkAll = false;
      }
    },
    getImgUrl(src) {
      return require("@/assets/img/course/" + src);
    },
    // 删除操作
    deleteByID(ids) {
      // console.log(ids);
      this.$parent.deleteData(ids);
    },
    // 打开弹框
    openDialog(val) {
      Bus.$emit("editByID", val);
    },
  },
};
</script>

<style lang="less" scoped>
.courselist-container {
  position: relative;
  header {
    height: 34px;
    line-height: 54px;
    margin-left: 30px;
    font-size: 16px;
    font-weight: bold;
  }
  .courselist {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .course-item {
      box-sizing: border-box;
      margin-top: 20px;
      padding: 0 12px;
      width: 290px;
      // border: 1px solid #000;
      background-color: #fff;
      // id栏
      .course-item-id {
        font-size: 12px;
        font-weight: bold;
        color: #000;
        width: 100%;
        height: 28px;
        // line-height: 28px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        input {
          margin: 8px 0 6px;
          width: 14px;
          height: 14px;
        }
      }
      // 课程信息
      .course-info {
        h2 {
          height: 14px;
          font-size: 14px;
          line-height: 14px;
          margin: 10px 0 0;
          font-weight: bold;
          letter-spacing: 1px;
        }
        p {
          font-size: 14px;
          letter-spacing: 1px;
          margin-top: 10px;
        }
      }
      // 上下架角标
      .img-box {
        position: relative;
        overflow: hidden;
        .subscript {
          position: absolute;
          top: -13px;
          right: -28px;
          display: block;
          width: 70px;
          height: 40px;
          font-size: 14px;
          color: #fff;
          text-align: center;
          line-height: 58px;
          transform: rotate(45deg);
        }
      }
      // 按钮
      .btnBox {
        margin: 18px 44px 10px;
        display: flex;
        justify-content: space-between;
        button {
          width: 68px;
          height: 24px;
          margin: 0;
          display: flex;
          align-items: center;
          padding: 0 12px 0 10px;
          color: #fff;
        }
        .editBtn {
          background-color: #438afe;
        }
        .deleteBtn {
          background-color: #b5b5b5;
        }
      }
    }
  }
  .bottom-box {
    position: absolute;
    left: 40px;
    margin-top: 14px;
    display: flex;
    align-items: center;
    .checkAll {
      font-size: 14px;
      height: 14px;
      line-height: 14px;
      font-weight: bold;
      height: auto;
      display: flex;
      align-items: center;
      margin-right: 10px;
      input {
        margin-right: 10px;
        width: 14px;
        height: 14px;
      }
    }
    /deep/.el-button {
      height: 14px;
      padding: 0;
      color: #333;
      font-size: 12px;
      line-height: 14px;
    }
  }
}
</style>
